<template>
    <el-main>
        <el-row :gutter="20">
            <el-col :span="24">
                <el-card v-if="regCard === true" class="box-card" style="min-height: 200px;">
                    <el-form status-icon :rules="regRules" ref="regForm" :model="optionReg" label-width="100px" size="mini">
                        <el-form-item label="用户名" prop="name">
                            <el-col :span="10">
                                <el-input size="small" v-model="optionReg.name" auto-complete="off">
                                    <i slot="prefix" class="el-icon-s-custom"></i>
                                </el-input>
                            </el-col>
                            <el-col :span="14">
                                <span style="color: #909399;">用户名只能输入2到20个字符之间英文和数字且首字母必须为英文字符</span>
                            </el-col>
                        </el-form-item>
                        <el-form-item label="邮箱" prop="email">
                            <el-col :span="10">
                                <el-input size="small" v-model="optionReg.email"
                                    auto-complete="off">
                                    <i slot="prefix" class="el-icon-message"></i>
                                </el-input>
                            </el-col>
                            <el-col :span="14">
                                <span style="color: #E6A23C;">邮箱需要有效邮箱，注册提交后需要邮箱激活</span>
                            </el-col>
                        </el-form-item>
                        <el-form-item label="输入密码" prop="password">
                            <el-col :span="10">
                                <el-input size="small" :type="passwordType" v-model="optionReg.password"
                                    auto-complete="off">
                                    <i slot="prefix" class="el-icon-lock"></i>
                                </el-input>
                            </el-col>
                            <el-col :span="14">
                                <span style="color: #F56C6C;">只能输入8到32个字符，且含至少1个大写字母，1个小写字母，1个数字和1个特殊字符</span>
                            </el-col>
                        </el-form-item>
                        <el-form-item label="确认密码" prop="password_confirmation">
                            <el-col :span="10">
                                <el-input size="small" :type="passwordType" v-model="optionReg.password_confirmation"
                                    auto-complete="off">
                                    <i slot="prefix" class="el-icon-lock"></i>
                                </el-input>
                            </el-col>
                            <el-col :span="14">
                                <span style="color: #67C23A;">再次确认密码，确保输入一致</span>
                            </el-col>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" size="small" @click.native.prevent="submitReg" class="login-submit">提交注册</el-button>
                            <el-button size="small" @click="resetReg">重 置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>

                <el-card v-if="regSuccessShow === true" class="box-card" style="min-height: 200px;">
                    <div class="layui-fluid larry-wrapper">
                        <div class="layui-row layui-col-space30">
                            <div class="layui-fluid">
                                <div class="layadmin-tips">
                                    <i class="el-icon-circle-check"></i>
                                    <div class="layui-text" style="color: green;">
                                    提交注册成功，请前往邮箱激活！
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </el-card>

            </el-col>
        </el-row>
    </el-main>
</template>

<script>
    export default {
        inject: ['reload'],
        data() {
            return {
                regCard: true,
                regSuccessShow: false,
                passwordType: 'password',
                optionReg: {
                    name: '',
                    email: '',
                    password: '',
                    password_confirmation: '',
                },
                regRules: {
                    name: [{
                        required: true,
                        message: "用户名不能为空",
                        trigger: "blur"
                    }],
                    email: [{
                        required: true,
                        message: "邮箱不能为空",
                        trigger: "blur",
                    }],
                    password: [{
                        required: true,
                        message: "密码不能为空",
                        trigger: "blur",
                    }],
                    password_confirmation: [{
                        required: true,
                        message: "确认密码不能为空",
                        trigger: "blur",
                    }],
                },
            }
        },
        methods: {
            submitReg() {
                const _this = this
                _this.$refs.regForm.validate(vaild=>{
                    if (vaild) {
                        const params = _this.optionReg
                        _this.axios.post(_this.$API.ZHIHUAPI.register, _this.qs.stringify(
                            params)).then((res) => {
                            res = res.data
                            if (res.code === 10000) {
                                _this.$message({
                                    showClose: false,
                                    message: res.message,
                                    type: 'success'
                                })
                                _this.regCard = false
                                _this.regSuccessShow = true
                            } else {
                                _this.$message({
                                    dangerouslyUseHTMLString: true,
                                    showClose: false,
                                    message: res.message,
                                    type: 'error'
                                })
                                return false
                            }
                        }).catch(() => {
                            _this.$message({
                                showClose: false,
                                message: '网络连接失败，请重试！',
                                type: 'error'
                            })
                        })
                    }
                })
            },
            resetReg() {
                this.$refs.regForm.resetFields();
            }
        }
    }
</script>

<style scoped>
    .layui-fluid {padding: 15px;}
    .layui-fluid {position: relative; margin: 0 auto; padding: 0 15px;}
    .layadmin-tips {margin-top: 30px;text-align: center;}
    .layadmin-tips > i {display: inline-block;font-size: 100px;color: #67C23A;}
    .layadmin-tips .layui-text { width: 100%; margin: 30px auto;padding-top: 20px;border-top: 2px solid #aaff7f;font-size: 16px;}
    .lay-carousel-div {width:100%;height:100%;display:flex;align-items:center;justify-content:center;overflow: auto;}
    .lay-carousel-div img{width:100%;height:auto;}
</style>
